package com.example.uibestpractice;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<Msg> msgList = new ArrayList<>();
    private EditText inputText;
    private Button send;

    private RecyclerView msgRecyclerView;

    private MsgAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initMessages();
        inputText = findViewById(R.id.input_text);
        send = findViewById(R.id.Send);
        msgRecyclerView = findViewById(R.id.msg_recycler_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        msgRecyclerView.setLayoutManager(linearLayoutManager);
        adapter = new MsgAdapter(msgList);
        msgRecyclerView.setAdapter(adapter);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    String content = inputText.getText().toString();
                    if(!"".equals(content)){
                        Msg msg = new Msg(content,Msg.TYPE_SENT);
                        msgList.add(msg);
                        adapter.notifyDataSetChanged();
                        msgRecyclerView.scrollToPosition(msgList.size()-1);
                        inputText.setText("");
                    }
            }
        });
    }

    private void initMessages(){
        Msg msg = new Msg("Hello guy",Msg.TYPE_RECEIVED);
        msgList.add(msg);
        Msg msg1 = new Msg("Hello.Who is that",Msg.TYPE_SENT);
        msgList.add(msg1);
        Msg msg2 = new Msg("This is Tom.Nice talking to you.",Msg.TYPE_RECEIVED);
        msgList.add(msg2);
    }
}
